from multiprocessing import Pool
from multiprocessing.context import TimeoutError
import time
import os


def f(x):
    print('-->', x, os.getpid())
    time.sleep(1)
    return x * x


if __name__ == '__main__':
    # # start 4 worker processes
    # with Pool(processes=4) as pool:
    #     # print "[0, 1, 4,..., 81]"
    #     print(pool.map(f, range(10)))
    #     # evaluate "f(20)" asynchronously
    #     res = pool.apply_async(f, (20,))      # runs in *only* one process
    #     print(res.get(timeout=1))

    pool1 = Pool(4)
    # res = pool1.apply_async(f, (100,))
    # print(res)
    # print(type(res))
    # print(res.get())
    # print(res.successful())
    for i in range(1, 10):
        res = pool1.apply_async(f, (i,))
    pool1.close()
    pool1.join()
    # time.sleep(12)
    print('over...')
